package test;

import java.util.HashSet;
import java.util.Set;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import common.HibernateUtil;
import model.Course;
import model.Student;
import model.Teacher;

public class MyApp {
	@Test
	public void testSave() {
		Session session=HibernateUtil.openSession();
		Student s=new Student();
		s.setName("He");
		s.setPhone("66666666666");
		s.setPwd("78");
		s.setGrade("Grade five");
		Transaction tx=session.beginTransaction();
		session.save(s);
		tx.commit();
	}
	
	@Test
	public void testMany2Many() {
		Student student=new Student();
		student.setId(1);
		
		Course course1=new Course();
		course1.setId(1);
		Course course2=new Course();
		course2.setId(2);
/*方法一：		
		Set<Course> courses=new HashSet<Course>();
		courses.add(course1);
		courses.add(course2);

		student.setCourses(courses);
*/		
		//方法二：必须得初始化
		student.getCourses().add(course1);
		student.getCourses().add(course2);
		
		Session session=HibernateUtil.openSession();
		session.beginTransaction();
		session.saveOrUpdate(student);
		session.getTransaction().commit();
		session.close();
		
		
	}
}
